package com.lul15834.orderfood.dao;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lul15834.orderfood.bean.CommentBean;

import com.lul15834.orderfood.db.DBUntil;
import com.lul15834.orderfood.until.Tools;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class CommentDao {
    public static SQLiteDatabase db= DBUntil.con;
    public static List<CommentBean> getCommentByBusiness(String id){

        List<CommentBean> list=new ArrayList<>();
        Cursor cursor=db.rawQuery("select * from d_comments where s_comment_business_id=?",new String[]{id});
        while (cursor.moveToNext()){
            CommentBean commentBean=new CommentBean();
            String idT= Tools.getResultString(cursor,"s_comment_id");
            commentBean.setSCommentId(idT);
            String userId= Tools.getResultString(cursor,"s_comment_user_id");
            commentBean.setSCommentUserId(userId);
            String businessId= Tools.getResultString(cursor,"s_comment_business_id");
            commentBean.setSCommentBusinessId(businessId);
            String con= Tools.getResultString(cursor,"s_comment_content");
            commentBean.setSCommentContent(con);
            String time= Tools.getResultString(cursor,"s_comment_time");
            commentBean.setSCommentTime(time);
            String score= Tools.getResultString(cursor,"s_comment_score");
            commentBean.setSCommentScore(score);
            String img= Tools.getResultString(cursor,"s_comment_img");
            commentBean.setSCommentImg(img);

            list.add(commentBean);
        }
        return list;
    }

    /**
     * 获取商家的平均分
     * @param account
     * @return
     */
    public static String getAvgScoreBusiness(String account){
        String data[]={account};
        String sql="select avg(s_comment_score) as score from d_comments where s_comment_business_id=?";
        Cursor rs=db.rawQuery(sql,data);
        if (rs.moveToNext()){
            String jg=Tools.getResultString(rs,"score");
            if (jg==null){
                return "0";
            }else {
                return jg;
            }
        }
        return "0";
    }

    /**
     * 评论内容
     * @param account
     * @param businessId
     * @param con
     * @param score
     * @param img
     * @return
     */
    public static int insertComment(String account,String businessId,String con,String score,String img){
        String id= UUID.randomUUID().toString().replace("-","");
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String time=simpleDateFormat.format(date);
        try {
            db.execSQL("INSERT INTO d_comments (s_comment_id,s_comment_user_id, s_comment_business_id, s_comment_content, s_comment_time, s_comment_score,s_comment_img) " +
                            "VALUES (?, ?, ?, ?, ?, ?,?)",
                    new Object[]{id, account, businessId, con, time,score, img});
            return 1;
        }catch (Exception e){
            return 0;
        }

    }
}
